package com.example.yyh.recorddemo.popupwindow;

import android.content.Context;
//import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.text.TextUtils.TruncateAt;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;

import com.example.yyh.recorddemo.R;
import com.example.yyh.recorddemo.base.BaseActivity;
import com.example.yyh.recorddemo.databinding.ActionReportSheetBinding;

/**
 * Created by yaoyihang on 2018/12/1.
 * 自定义弹窗菜单
 */
public class CustomActionSheet extends PopupWindow {

    private PupopListener mListener;
    private View mContentView;
    private TextView mTvCancel;
    private Context mContext;
    private boolean isDismissed = false;
    private View mLayoutContent;
    private int num = 100;

    private CustomActionSheet(View contentView, int width, int height,
                              boolean focusable) {
        super(contentView, width, height, focusable);
    }


    public static CustomActionSheet getInstance(Context context) {
        ActionReportSheetBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.action_report_sheet, null, false);

        final CustomActionSheet actionSheet = new CustomActionSheet(binding.getRoot(),
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);

        actionSheet.mContentView = binding.getRoot();
        actionSheet.mLayoutContent = binding.layoutContent;
        actionSheet.mTvCancel = binding.tvCancel;
        actionSheet.mContext = context;

        actionSheet.mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                actionSheet.dismiss();
            }
        });
        actionSheet.mContentView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    actionSheet.dismiss();
                }
                return true;
            }
        });
        actionSheet.mLayoutContent
                .setOnTouchListener(new View.OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        return true;
                    }
                });

        actionSheet.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return actionSheet;
    }

    public void show() {
        if (((BaseActivity) mContext).getWindow().isActive()) {
            showAtLocation(((BaseActivity) mContext).getWindow().getDecorView(), Gravity.BOTTOM,
                    0, 0);

            Animation animation = AnimationUtils.loadAnimation(mContext,
                    R.anim.actionsheet_in);
            animation.setFillEnabled(true);
            animation.setFillAfter(true);
            mLayoutContent.startAnimation(animation);
        } else {
            new Handler().postDelayed(new Runnable() {

                @Override
                public void run() {
                    if (((BaseActivity) mContext).getWindow().isActive()) {
                        showAtLocation(((BaseActivity) mContext).getWindow().getDecorView(),
                                Gravity.BOTTOM, 0, 0);

                        Animation animation = AnimationUtils.loadAnimation(
                                mContext, R.anim.actionsheet_in);
                        animation.setFillEnabled(true);
                        animation.setFillAfter(true);
                        mLayoutContent.startAnimation(animation);
                    }
                }
            }, 600);
        }

    }

    public void dropCancle() {
        mTvCancel.setVisibility(View.GONE);
    }


    public void setNum(int num) {
        this.num = num;
    }

    public void setItems(String[] items) {
        LinearLayout layoutBtn = mContentView.findViewById(R.id.layout_btn);
        int size = items.length;


        for (int i = 0; i < size; i++) {
            final int index = i;
            final String item = items[i];
            LayoutParams p = new LayoutParams(LayoutParams.MATCH_PARENT, 120);
            LinearLayout ll = new LinearLayout(mContext);
            ll.setOrientation(LinearLayout.HORIZONTAL);
            ll.setGravity(Gravity.CENTER);
            ll.setLayoutParams(p);

            TextView btn = new TextView(mContext);
            btn.setGravity(Gravity.CENTER);
            btn.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
            btn.setEllipsize(TruncateAt.END);
            btn.setSingleLine(true);

            btn.setText(item);


            LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, 120);
            btn.setLayoutParams(lp);

            ll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    dismiss();
                    if (mListener != null) {
                        mListener.onItemClicked(index, item);
                    }
                }
            });

            View view = new View(mContext);
            LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    1);
            view.setBackgroundColor(Color.parseColor("#e2e2e2"));
            view.setLayoutParams(params);

            ll.addView(btn);
            layoutBtn.addView(ll);
            layoutBtn.addView(view);
        }
    }

    public interface PupopListener {
        void onItemClicked(int index, String item);
    }

    public void setListener(PupopListener listener) {
        this.mListener = listener;
    }

    @Override
    public void dismiss() {
        if (isDismissed) {
            return;
        }
        isDismissed = true;
        Animation animation = AnimationUtils.loadAnimation(mContext,
                R.anim.actionsheet_out);
        animation.setFillEnabled(true);
        animation.setFillAfter(true);
        animation.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation arg0) {

            }

            @Override
            public void onAnimationRepeat(Animation arg0) {

            }

            @Override
            public void onAnimationEnd(Animation arg0) {
                mLayoutContent.setVisibility(View.INVISIBLE);
                new Handler().post(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            superDismiss();
                        } catch (Exception e) {
                        }
                    }
                });
            }
        });
        mLayoutContent.startAnimation(animation);
    }

    public void superDismiss() {
        super.dismiss();
    }
}
